import * as R from 'ramda';
import { Reader } from './vendor/ramda-fantasy';
import U from './index';
//翻页处理
// 'currentPageData' 是固定值，翻页的相关属性都放在里面
export const turnPage = (request, { pageData = 'pageData', pageNum = 'pageNum', pageSize = 20, isLastPage = 'isLastPage', isShowPageLoading = 'isShowPageLoading' } = {}) => Reader(
  that => R.when(
    R.both(R.equals(that.data.currentPageData[isLastPage]), R.equals(that.data.currentPageData[isShowPageLoading])),
    R.compose(
      U.fork,
      R.map(
        R.compose(
          currentPageData => that.setData({
            // 总页码等于当前页码说明是最后一页
            ['currentPageData.' + isLastPage]: currentPageData.page_total === currentPageData.page_num,
            ['currentPageData.' + pageData]:  R.concat(that.data.currentPageData[pageData], currentPageData.list),
            ['currentPageData.' + pageNum]: that.data.currentPageData[pageNum] + 1,
          }),
          R.prop('data')
        )
      ),
      () => U.handleRequest({ loading: {
          that,
          name: 'currentPageData.' + isShowPageLoading,
        }
      }, request)
    )
  )(false)
)
